/*
 * ThreadHeaderComparator.java
 *
 * Created on 2008/09/06 +0900
 *
 * $Revision: 58 $
 * $Date: 2008-12-06 08:59:45 +0000 (Sat, 06 Dec 2008) $
 */

package org.gikolet.bbs.data;

import org.gikolet.framework.util.Assert;
import org.gikolet.framework.util.Comparator;
import org.gikolet.framework.util.StringComparator;

/**
 * @author tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 58 $ $Date: 2008-12-06 08:59:45 +0000 (Sat, 06 Dec 2008) $
 */
public class ThreadHeaderComparator implements Comparator {
    public static final int SUBJECT = 0;
    public static final int KEY = 1;
    public static final int RES_COUNT = 2;

    private int comparateType;
    private Comparator comparator;

    public ThreadHeaderComparator(int comparateType) {
	switch (comparateType) {
	case KEY:
	case SUBJECT:
	    this.comparator = new StringComparator();
	    break;
	case RES_COUNT:
	    break;
	default:
	    Assert.fail();
	}

	this.comparateType = comparateType;
    }

    /**
     *
     * @see org.gikolet.framework.util.Comparator#compare(java.lang.Object,
     *      java.lang.Object)
     */
    public int compare(Object o1, Object o2) {
	return compare((ThreadHeader) o1, (ThreadHeader) o2);
    }

    public int compare(ThreadHeader threadHeader1, ThreadHeader threadHeader2) {
	Assert.isNotNull(threadHeader1);
	Assert.isNotNull(threadHeader2);

	if (this.comparateType == RES_COUNT) {
	    return threadHeader1.getResCount() - threadHeader2.getResCount();
	}

	Object o1 = null;
	Object o2 = null;

	switch (this.comparateType) {
	case KEY:
	    o1 = threadHeader1.getKey();
	    o2 = threadHeader2.getKey();
	    break;

	case SUBJECT:
	    o1 = threadHeader1.getSubject();
	    o2 = threadHeader2.getSubject();
	    break;
	default:
	    Assert.fail();
	}
	return this.comparator.compare(o1, o2);
    }
}
